package com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_rc_show_news.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

class RcShowNewsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_rc_show_news)
        title = "新闻详情"


        intent.getStringExtra("id")?.toInt().let {
            // 填充新闻信息
            setIdShow(it)
            // 发表评论
            setRcAdd(it)
        }
    }

    /**
     * 发表评论
     */
    private fun setRcAdd(id: Int?) {
        rcAddBtn.setOnClickListener {
            if (getToken().isNotEmpty()) {
                // 判断评论内容是否为空
                if (rcAddContent.text.toString().isNotEmpty()) {
                    // 开启协程发起网络
                    CoroutineScope(Dispatchers.IO).launch {
                        api.getRcAdd(getToken(), JSONObject().apply {
                            put("newsId", id.toString())
                            put("content", rcAddContent.text.toString())
                        }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                            // 判断是否发表成功
                            if (this.code == "200"){
                                runOnUiThread {
                                    this.msg.show()
                                    rcAddContent.setText("")
                                }
                            }else runOnUiThread {
                                this.msg.show()
                            }
                        }
                    }
                } else "你还没输入内容呢！".show()

            } else "请先登录！".show()
        }
    }

    /**
     * 填充新闻信息
     */
    private fun setIdShow(id: Int?) {
        // 判断token是否为空
        if (getToken().isNotEmpty()) {
            // 开启协程发起网络请求
            CoroutineScope(Dispatchers.IO).launch {
                // 获取新闻信息详情
                api.getRcShowNews(getToken(), id!!).apply {
                    // 判断是否请求成功！
                    if (this.code == 200) {
                        runOnUiThread {
                            // 填充数据
                            this.data?.let {
                                rcShowTitle.text = it.title
                                rcShowUser.text = "发布人: ${it.author}"
                                rcShowTitle.text = "发布时间: ${it.createTime}"
                                glide(url + it.imgUrl, rcShowImage)
                                rcShowContent.text = Html.fromHtml(it.content)
                            }
                        }
                    } else runOnUiThread {
                        this.msg?.show()
                    }
                }
            }
        } else "请先登录！".show()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}